package com.kanqiubao.kq.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kanqiubao.kq.R;
import com.kanqiubao.kq.http.HttpListener;
import com.kanqiubao.kq.utils.ActivityStartAndFinshAnimation;
import com.kanqiubao.kq.utils.StringUtil;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import okhttp3.Call;
import okhttp3.Response;

public abstract class BaseActivity<T> extends FragmentActivity implements HttpListener<T> {
    protected boolean isNavigationClick;
    protected boolean isWelCome;
    protected boolean isToolbarTextColorWrite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!isWelCome) {
            initStatuBar();
        }
        getSchameData();
//        setToolbarTran();
        super.onCreate(savedInstanceState);
        if (getActivitylayout() != 0) {
            setContentView(getActivitylayout());
        }
        onActivityCreate(savedInstanceState);// fragment初始化必须在onCreate之后
        setMyTitle();
    }

    /**
     * 观看视频回调
     */
//    protected void getData(String type) {
//        AdvertRequestBean bean = new AdvertRequestBean();
//        bean.setUserid(SharedPreferencesUtil.getInstance().getToken());
//        bean.setSendid(0);
//        bean.setSn(null);
//        bean.setType(type);
//        bean.setSign();
//        OkGo.post(HttpUrls.videoSee).tag(this)
//                .upJson(Convert.toJson(bean))
//                .execute(new StringResponeListener() {
//                    @Override
//                    public void onSuccess(String s, Call call, Response response) {
//                        ErrorBean bean = Convert.fromJson(s, ErrorBean.class);
//                        if (bean.IsSuccess()) {
//                            EventBus.getDefault().post(new AppConstant().setType(AppConstant.CHANGE_INFO));
//                        }
//                    }
//                });
//    }

    /**
     * 获取跳转协议的id
     */
    private void getSchameData() {
        try {
            Uri data = getIntent().getData();
            if (data != null) {
                String id = data.getQueryParameter("id");
                if (!StringUtil.isEmptyString(id)) {
                    getIntent().putExtra("id", id);
                }
            }
        } catch (Exception e) {

        }

    }

    protected void setMyTitle() {
        TextView txt = findViewById(R.id.app_title_txt_id);
        if (txt != null) {
            txt.setText(getTitleText());
        }
    }

    /**
     * 设置状态栏全透明
     */
    private void setToolbarTran() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
            int tag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            if (!isToolbarTextColorWrite) {
                tag = tag | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;//状态栏白色背景，灰色字体，其他的都是白色字体
//                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
            window.getDecorView().setSystemUiVisibility(tag);//状态栏白色背景，灰色字体，其他的都是白色字体);
        }
    }

    private void initStatuBar() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            if (AppConfig.getNavigationBarHeight(this) > 0) {
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//                if (isNavigationClick) {
//                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//                }
//            } else {
//                getWindow().clearFlags(
//                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//                getWindow().getDecorView().setSystemUiVisibility(
//                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            }
//        }
    }

    /**
     * 初始化
     *
     * @param savedInstanceState
     */
    protected abstract void onActivityCreate(Bundle savedInstanceState);

    /**
     * 界面布局
     *
     * @return
     */
    protected abstract int getActivitylayout();

    /**
     * 界面标题
     *
     * @return
     */
    protected abstract String getTitleText();

    protected void requestData() {
        shouCustomProgressDialog();
    }

    protected <V> V findView(int id) {
        return (V) findViewById(id);
    }


    public void shouCustomProgressDialog() {
        if (isFinishing()) {
            return;
        }
        if (progressDialog == null) {
            progressDialog = new Dialog(this, R.style.progress_dialog);
            progressDialog.setContentView(R.layout.request_dialog);
            progressDialog.setCancelable(true);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
        progressDialog.show();
    }

    protected Dialog progressDialog;

    public void shouCustomProgressDialog(String message) {
        if (progressDialog == null) {
            progressDialog = new Dialog(this, R.style.progress_dialog);
            progressDialog.setContentView(R.layout.request_dialog);
            progressDialog.setCancelable(true);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
        if (progressDialog.isShowing()) {
            return;
        }
        if (!StringUtil.isEmptyString(message)) {
            TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
            msg.setVisibility(View.VISIBLE);
            msg.setText(message);
        }
        progressDialog.show();
    }

    public void dissmisCustomProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }


    /**
     * 启动新的activity
     *
     * @param cls
     */
    public void startMyActivity(Intent in, Class<?> cls) {
        in.setClass(this, cls);
        // in.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(in);
        ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
    }

    public void startMyActivityResult(Intent in, Class<?> cls) {
        in.setClass(this, cls);
        startActivityForResult(in, 101);
        ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
    }

    public void startMyActivity(Class<?> cls) {
        Intent in = new Intent();
        in.setClass(this, cls);
        startActivity(in);
        ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
    }

    public void startMyActivity(Intent in) {
        startActivity(in);
        ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
    }

    public void startMyResuletActivity(Intent in) {
        startActivityForResult(in, 0);
        ActivityStartAndFinshAnimation.ActivityStartAnimation(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void click(View v) {
        if (v.getId() == R.id.app_title_back_root) {
            finish();
        }
    }

    @Override
    public void finish() {
//		boolean start = getIntent().getBooleanExtra("push", false);
//		if (start) {
//			// 推送过来的
//			start = TDevice.isForeground(this, "com.app.ui.activity.MainActivity");
//			if (!start) {
//				// 没有打开的MainActivity
//				startMyActivity(MainActivity.class);
//			}
//		}
        super.finish();
        ActivityStartAndFinshAnimation.ActivityFinish(this);
    }


    @Override
    public void onSuccess(T paramT, Call paramCall, okhttp3.Response paramResponse) {

    }

    @Override
    public void onCacheSuccess(T t, Call call) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onError(Call call, okhttp3.Response response, Exception e) {
        error(response);
    }

    public void error(okhttp3.Response response) {
        dissmisCustomProgressDialog();
        if (response == null) {
            return;
        }
    }

    @Override
    public void onCacheError(Call call, Exception e) {

    }

    @Override
    public void parseError(Call call, Exception e) {

    }

    @Override
    public void onAfter(T t, Exception e) {

    }

}
